AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
include("shared.lua")

function ENT:SpawnFunction(ply, tr)
	local SpawnPos = tr.HitPos + tr.HitNormal * 42

	local ent = ents.Create(self.ClassName)
	ent:SetPos(SpawnPos)
	ent:Spawn()
	ent:Activate()

	return ent
end

function ENT:Initialize()
	self:SetModel(self.Model)
	local c = self.Color
	self:SetColor(255,255,255,255)
	self:PhysicsInit(SOLID_VPHYSICS)
	self:SetMoveType(MOVETYPE_VPHYSICS)
	self:SetSolid(SOLID_VPHYSICS)
	self:PhysWake()
end

function ENT:Use(activator, caller)
	activator:GiveAmmo(self.AmmoAmmount,self.AmmoType)
	self:Remove()
end

function ENT:OnTakeDamage(dmg)
	if self.HP then
		self.HP = self.HP - dmg:GetDamage()

		if self.HP <= 0 then
			local effectdata = EffectData()
				effectdata:SetOrigin(self:GetPos())
				effectdata:SetMagnitude(2)
				effectdata:SetScale(2)
				effectdata:SetRadius(3)
			util.Effect("Sparks", effectdata)
			
			self:Remove()
		end
	end
end

function DropPlayerAmmo(ply)
	 for ent, tbl in pairs(scripted_ents.GetSpawnable()) do
        if tbl.Base == "base_ammo" then
		local ammocount = ply:GetAmmoCount(tbl.AmmoType)
			if(ammocount > 1) then
			local ammo = ents.Create(tbl.Classname)
				ammo:SetPos(ply:GetPos()+Vector(math.random(1,15),math.random(1,15),math.random(1,15)))
				if(ammocount > 250) then ammocount = 250 end
				ammo.AmmoAmmount = ammocount
			ammo:Spawn()
			end
		end
    end
end
hook.Add("DoPlayerDeath", "DropPlayerAmmo", DropPlayerAmmo)

function ENT:CanRecycle()
	return true
end
